home *** CD-ROM | disk | FTP | other *** search
- #
- # Preferences.py
- # JunkMatcher
- #
- # Created by Benjamin Han on 2/1/05.
- # Copyright (c) 2005 Benjamin Han. All rights reserved.
- #
-
- # This program is free software; you can redistribute it and/or
- # modify it under the terms of the GNU General Public License
- # as published by the Free Software Foundation; either version 2
- # of the License, or (at your option) any later version.
-
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
-
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
- #!/usr/bin/env python
-
- import string
-
- from consts import *
- from utilities import *
-
-
- class Preferences:
- """
- Global Preferences of JunkMatcher
- ---------------------------------
- fn: filename of the prefs file
- siteLimit: (int) the size limit for SiteDB
- mode: (int) matching mode for Matcher
- modeArgs: (list of strings) optional arguments for matching mode
- junkMessage: (bool) whether to mark matched messages as junk
- recycleDays: (int) # of days to periodically recycle log
- """
- def __init__ (self, fn):
- self.fn = fn
- self.load()
-
- def __getattr__ (self, name):
- # if a name is referenced but does not yet exist, try to query the defaults
- defaultPrefs = Preferences('%sprefs' % DEFAULTS_PATH)
- for k in filter(lambda k: not self.__dict__.has_key(k), defaultPrefs.__dict__.keys()):
- setattr(self, k, getattr(defaultPrefs, k))
-
- if self.__dict__.has_key(name): return self.__dict__[name]
- else: raise AttributeError(name)
-
- def load (self):
- mode = 0
- for l in filter(lambda l:len(l) and l[0] != '#', map(string.strip, openFile(self.fn))):
- if mode == 0:
- self.siteLimit = int(l)
- elif mode == 1:
- l = l.split(' ')
- self.mode = int(l[0])
- self.modeArgs = l[1:]
- elif mode == 2:
- self.junkMessage = (l[0] == '1')
- elif mode == 3:
- self.recycleDays = int(l)
-
- mode += 1
-
- def writeToFile (self):
- openFile(self.fn, 'w').write('%d\n%d %s\n%d\n%d' %\
- (self.siteLimit,
- self.mode, encodeText(' '.join(self.modeArgs)),
- int(self.junkMessage),
- int(self.recycleDays)))
-
-
- if __name__ == '__main__':
- prefs = Preferences('%sprefs' % CONF_PATH)
- #print prefs.foo
-